package com.example.bookmanage.controller;

import com.example.bookmanage.domain.TableDataInfo;
import com.example.bookmanage.domain.book.dto.BookDTO;
import com.example.bookmanage.domain.book.dto.BookEditDTO;
import com.example.bookmanage.domain.book.dto.BookQueryDTO;
import com.example.bookmanage.domain.book.vo.BookVO;
import com.example.bookmanage.result.Result;
import com.example.bookmanage.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "图书管理")
@RestController
@RequestMapping("/book")
public class BookController extends BaseController {

    @Autowired
    private BookService bookService;

    /**
     * 查询图书列表
     *
     * @param bookQueryDTO
     * @return
     */
    @Operation(summary = "查询图书列表")
    @GetMapping("/list")
    public TableDataInfo list(@ParameterObject BookQueryDTO bookQueryDTO) {
        List<BookVO> list = bookService.list(bookQueryDTO);
        return getTableDataInfo(list);
    }

    /**
     * 添加图书
     *
     * @param bookDTO
     * @return
     */
    @Operation(summary = "添加图书")
    @PostMapping("/add")
    public Result<?> add(@RequestBody BookDTO bookDTO) {
        return toResult(bookService.addBook(bookDTO));
    }

    /**
     * 修改图书
     *
     * @param BookEditDTO
     * @return
     */
    @Operation(summary = "修改图书")
    @PutMapping("/update")
    public Result<?> update(@RequestBody BookEditDTO BookEditDTO) {
        return toResult(bookService.updateBook(BookEditDTO));
    }

    /**
     * 删除图书
     *
     * @param bookId
     * @return
     */
    @Operation(summary = "删除图书")
    @DeleteMapping("/delete/{bookId}")
    public Result<?> delete(@PathVariable("bookId") Long bookId) {
        return toResult(bookService.deleteById(bookId));
    }

}
